home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
Sherlock 2.0
/
DevLibSrc
/
Main_DevLib
/
LIBes.c
< prev
next >
Wrap
Text File
|
1996-04-07
|
8KB
|
528 lines
/*
devlib: General stream-oriented standard error routines.
All routines output various items to the "error stream."
The semantics of this stream are application-defined via
the routines es, ecnl, ecnls and ecblanks.
source: LIBes.c
started: November 4, 1993.
version:
March 14, 1996.
Use Microseconds() in ticks2msec for Symantec or Metrowerks compilers.
March 5, 1995.
Allowed sysbeep() call for Metrowerks compiler.
February 5, 1996.
Support for Microseconds() toolbox call.
November 30, 1995.
`es_assert_failed' and `es_internal_err' are now application-specific.
November 8, 1995.
Bug fix: epadhex does not truncate if the field is zero.
Call cvt routines instead of sprintf.
Made ejustify visible for use by LIBfloat.c.
Moved edouble and epaddouble to LIBfloat.c.
September 26, 1995
bug fix to etrunc.
*/
#include <LIBlib.h>
#include <LIBcvt.h> /* For when ES_USE_CVT is #defined. */
#include <LIBes.h>
#include <ctype.h> /* For isprint */
#include <string.h> /* For strlen */
#include <stdio.h> /* For FILE */
/*
#define ES_USE_CVT to use routines in LIBcvt.c instead of sprintf.
This is a way of avoiding sprintf (and the floating point library).
#undef ES_USE_CVT to avoid all calls to routines in LIBcvt.c.
*/
#define ES_USE_CVT
/*
Size of the conversion buffers.
*/
#define ES_BUF_SIZE 200
/*
Define global variables.
*/
long io_line_count = 0; /* Number of chars in current output line. */
int es2os_flag = FALSE; /* TRUE: all call to es stream go to os stream. */
void
eangle(char * s)
{
echar('<'); es(s); echar('>');
}
void
ebell(void)
{
#if defined(THINK_C) || defined(SYMANTEC_C) || defined(__MWERKS__)
SysBeep(20);
#else
#if 0 /* We don't want to "print" this character. */
echar('\a');
#endif
#endif
}
void
eblank(void)
{
echar(' ');
}
void
eblanks(register int n)
{
int i;
for(i = 0; i < n; i++) {
eblank();
}
}
void
ebool(bool b)
{
epadbool(b, 0);
}
void
ebracket(char * s)
{
echar('['); es(s); echar(']');
}
void
echar(int c)
{
char buffer[2];
buffer [0] = c;
buffer [1] = '\0';
es(buffer);
}
void
ecs(void)
{
es(", ");
}
void
ecurly(char * s)
{
echar('{'); es(s); echar('}');
}
void
efrac(long a1, long a2)
{
epadfrac(a1, a2, 0);
}
void
ehex(long hex)
{
epadhex(hex, 0);
}
void
ehexchar(long hex)
{
epadhex(hex & 0xff, 0);
}
void
eint(int i)
{
epadint(i, 0);
}
void
ejustify(char * buffer, int length, int field_length)
{
if (field_length < 0) {
/* Left justify */
es(buffer);
eblanks(-field_length - length);
}
else {
/* Right justify */
eblanks(field_length - length);
es(buffer);
}
}
void
elong(long l)
{
epadlong(l, 0);
}
void
elp(void)
{
es("(");
}
void
emsec(long ticks)
{
epadmsec(ticks,0);
}
void
enl(void)
{
es("\n");
}
void
epadbool(bool b, int field)
{
if (b) {
ejustify("TRUE", 4, field);
}
else {
ejustify("FALSE", 5, field);
}
}
void
epadchar(char c, int field)
{
char buffer[2];
buffer[0] = c;
buffer[1] = '\0';
ejustify(buffer, 1, field);
}
void
epadfrac(long a1, long a2, int field)
{
char buf [ES_BUF_SIZE];
if (a2 == 0) {
es("0.0");
return;
}
#ifdef ES_USE_CVT
cvt_frac(buf, ES_BUF_SIZE, a1, a2);
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "%ld.%ld", a1/a2, ((10 * a1)/a2) % 10), field);
#endif
}
/*
Output a long in hex format, justified in a field of the given length.
*/
#define abs(a) ((a < 0) ? -(a) : (a))
void
epadhex(long hex, int field)
{
char buf [ES_BUF_SIZE];
int n = 0;
int len = 0;
#ifdef ES_USE_CVT
cvt_hex(buf, ES_BUF_SIZE, hex);
#else
sprintf(buf, "%lx", hex);
#endif
len = strlen(buf);
if (len >= abs(field) && field != 0) {
/* Truncate to the indicated number of digits. */
es(buf + len - abs(field));
}
else if (field > 0) {
/* Zero pad on the left. */
n = field - len;
while (n-- > 0) {
echar('0');
}
es(buf);
}
else {
/* blank pad on the right. */
ejustify(buf, len, field);
}
}
/*
Output an int, justified in a field of the given length.
*/
void
epadint(int i, int field)
{
char buf [ES_BUF_SIZE];
#ifdef ES_USE_CVT
cvt_int(buf, ES_BUF_SIZE, i);
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "%d", i), field);
#endif
}
/*
Output blanks to pad an item of size length in a field of field_length.
*/
void
epadlen(int item_length, int field_length)
{
eblanks(field_length - item_length);
}
/*
Output a long, justified in a field of the given length.
*/
void
epadlong(long l, int field)
{
char buf [ES_BUF_SIZE];
#ifdef ES_USE_CVT
cvt_long(buf, ES_BUF_SIZE, l);
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "%ld", l), field);
#endif
}
/*
Convert ticks to msec.
A tick is about 1.28 msec.
*/
void
epadmsec(long ticks, int field)
{
epadlong(ticks2msec(ticks), field);
}
/*
Output a parenthesized string, justified in a field of a given length.
*/
void
epadparen(char * s, int field)
{
char buf [ES_BUF_SIZE];
#ifdef ES_USE_CVT
cvt_paren(buf, ES_BUF_SIZE, s);
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "(%s)", s), field);
#endif
}
/*
Output a parenthesized long, justified in a field of a given length.
*/
void
epadparenlong(long a, int field)
{
char buf [ES_BUF_SIZE];
#ifdef ES_USE_CVT
strcpy(buf, "(");
cvt_long(buf+1, ES_BUF_SIZE-2, a);
strcat(buf, ")");
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "(%ld)", a), field);
#endif
}
/*
Output a pointer justified in a field of the given length.
*/
void
epadptr(void * p, int field)
{
char buf [ES_BUF_SIZE];
#ifdef ES_USE_CVT
cvt_ptr(buf, ES_BUF_SIZE, p);
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "%p", p), field);
#endif
}
/*
Output a string justified in a field of the given length.
*/
void
epads(char * s, int field)
{
if (s != NULL) {
ejustify(s, strlen(s), field);
}
else {
ejustify(" ", 1, field);
}
}
/*
Output an unsigned int, justified in a field of the given length.
*/
void
epaduint(uint ui, int field)
{
char buf [ES_BUF_SIZE];
#ifdef ES_USE_CVT
cvt_uint(buf, ES_BUF_SIZE, ui);
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "%u", ui), field);
#endif
}
/*
Output an unsigned long, justified in a field of the given length.
*/
void
epadulong(ulong ul, int field)
{
char buf [ES_BUF_SIZE];
#ifdef ES_USE_CVT
cvt_ulong(buf, ES_BUF_SIZE, ul);
ejustify(buf, strlen(buf), field);
#else
ejustify(buf, sprintf(buf, "%lu", ul), field);
#endif
}
void
eparen(char * s)
{
echar('('); es(s); echar(')');
}
void
eplural(long n)
{
if (n != 1) {
es("s");
}
}
void
epstring(char * p)
{
int n = *p++;
while(n--) {
echar(*p++);
}
}
void
eptr(void * p)
{
epadptr(p, 0);
}
void
equote(char * s)
{
echar('"'); es(s); echar('"');
}
void
eret(void)
{
es("returns: ");
}
void
erpnl(void)
{
es(")\n");
}
void
etab(void)
{
es("\t");
}
void
etabs(register int tab_count)
{
while (tab_count-- > 0) {
es("\t");
}
}
void
etrunc(char * s, int n)
{
while (*s && n-- > 0) {
echar(*s++); /* bug fix: 9/26/95: was *s */
}
}
void
euint(uint ui)
{
epaduint(ui, 0);
}
void
eulong(ulong ul)
{
epadulong(ul, 0);
}
/*
Convert from ticks to msec.
*/
long
ticks2msec(long ticks)
{
double dticks = ticks;
#if defined(TUPLE_C)
/* A tick is about 1.28 msec. */
// return (long) ((dticks * 1.28) / 1000);
return (long) (dticks * 0.00128); // work around compiler bug
#elif defined(THINK_C) || defined(SYMANTEC_C) || defined(__MWERKS__) || defined(applec)
/* A tick is 1 microsec. */
return (long) (dticks/1000.0);
#else
/* A tick is 1/60 sec. */
// return (long) ((dticks * 1000) / 60);
return (long) (dticks * 16.6666666666); // work around compiler bug
#endif
}